using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class SpecificationController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Subclassify> _subclassifyController;
        private IRepository<Specification> _SpecificationRepository;
        private IRepository<Specificationvalue> _SpecificationvalueRepository;
        private TokenParameter _tokenParameter;

        public SpecificationController(IConfiguration configuration,
         IRepository<Specification> SpecificationRepository,
         IRepository<Subclassify> subclassifyController,
         IRepository<Specificationvalue> SpecificationvalueRepository
        )
        {
            _configuration = configuration;
            _subclassifyController = subclassifyController;
            _SpecificationRepository = SpecificationRepository;
            _SpecificationvalueRepository = SpecificationvalueRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //列出细分类列表
        [HttpGet]
        public dynamic Get()
        {
            var Specification = _SpecificationRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = Specification },
                Msg = "获取列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var specification = _SpecificationRepository.GetById(id);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = specification,
                Msg = "查询指定用户成功"
            });
        }

        [HttpPost]
        public dynamic Post(CreateSpecification newSpecification)
        {
            var specification = _SpecificationRepository.Table.ToList();
            var Specificationname = newSpecification.Specificationname.Trim();
            var SubclassifyId = newSpecification.SubclassifyId;
            var specificationaa = specification.Where(x => x.Specificationname == Specificationname).ToList();
            var specificationss = specification.Where(x=>x.SubclassifyId == SubclassifyId).ToList();
            if(specificationss.Count == 0){
                var Specification = new Specification
                {
                    Specificationname = newSpecification.Specificationname,
                    SubclassifyId = newSpecification.SubclassifyId,
                };
                _SpecificationRepository.Insert(Specification);
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = Specification,specificationss,
                    Msg = "添加规格成功"
                });
            }else{
            if (specificationaa.Count == 0 )
            {
                var Specification = new Specification
                {
                    Specificationname = newSpecification.Specificationname,
                    SubclassifyId = newSpecification.SubclassifyId,
                };
                _SpecificationRepository.Insert(Specification);
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = Specification,specificationss,
                    Msg = "添加规格成功"
                });
            }
            else
            {
                var tmp = new
                {
                    Code = 1004,
                    Data = specificationaa,
                    Msg = "有同样的了"
                };
                return JsonHelper.Serialize(tmp);
            }
            }


        }

        [HttpPut("{id}")]
        public dynamic Put(int id, CreateSpecification updateSpecification)
        {
            var Specificationname = updateSpecification.Specificationname.Trim();
            var specification = _SpecificationRepository.GetById(id);
            if (specification == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "查询不到该用户，无法更新，请重试"
                });
            }

            specification.Specificationname = updateSpecification.Specificationname;
            // user.IsActived= updateUser.IsActived;

            _SpecificationRepository.Update(specification);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = specification,
                Msg = "更新成功"
            });


        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _SpecificationRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }
        //连接分类表和细分类表
        [HttpGet("ggandggz/{id}"), Route("ggandggz")]
        public dynamic ClassifyandSubclassify(int id)
        {
            var Specificationvalue = _SpecificationvalueRepository.Table.ToList();
            var Specification = _SpecificationRepository.Table.ToList();
            var Subclassify = _subclassifyController.Table.ToList();
            var query = from a in Subclassify
                        join b in Specification on a.Id equals b.SubclassifyId
                        join c in Specificationvalue on b.Id equals c.SpecificationId
                        select new
                        {
                            SubclassifyId = a.Id,
                            SpecificationId = b.Id,
                            SpecificationvalueId = c.Id,
                            Specificationvaluename = c.Specificationvaluename
                        };
            var specification = Specification.Where(x => x.SubclassifyId == id).ToList();
            var specificationvalue = query.Where(x => x.SubclassifyId == id).ToList();
            return new
            {
                Code = 1000,
                Data = new { Data = specification, Pager = specificationvalue },
                Msg = "查询指定用户Id成功"
            };


        }
        //连接分类表和细分类表
        [HttpGet("guigezhi/{name}"), Route("guigezhi")]
        public dynamic Specificationname(string name)
        {
            var Specificationvalue = _SpecificationvalueRepository.Table.ToList();
            var Specification = _SpecificationRepository.Table.ToList();
            var query = from a in Specification
                        join b in Specificationvalue on a.Id equals b.SpecificationId
                        select new
                        {
                            SpecificationId = a.Id,
                            Specificationname=a.Specificationname,
                            SpecificationvalueId = b.Id,
                            Specificationvaluename = b.Specificationvaluename
                        };
            var specificationvalue = query.Where(x => x.Specificationvaluename == name).ToList();
            return new
            {
                Code = 1000,
                Data =specificationvalue,
                Msg = "查询指定用户Id成功"
            };


        }
    }

}
